﻿using System; 
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Newtonsoft.Json;

public partial class AjaxArrayJson : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public static string ServerMethod(HttpContext context, string UniqueID, string Data)
    {
        if (Data == "")
        {
            SharedData sd = new SharedData();
            sd.SharedDouble = 6;
            sd.SharedInt = 5;
            sd.SharedString = "Hello World";
            sd.Children = new List<SharedChild>();
            SharedChild c1 = new SharedChild();
            c1.SharedChildString = "Im a child";
            sd.Children.Add(c1);
            return Newtonsoft.Json.JsonConvert.SerializeObject(sd);
        }
        else
        {
            SharedData data = (Newtonsoft.Json.JsonConvert.DeserializeObject(Data, typeof (SharedData)) as SharedData);
            if (data != null)
                return "{\"result\":\"GOOD\"}";
            else
                return "{\"result\":\"BAD\"}";
        }
        return "";
    }
}

public class SharedData
{
    public string SharedString { get; set; }
    public double SharedDouble { get; set; }
    public int SharedInt { get; set; }
    public System.Collections.Generic.List<SharedChild> Children { get; set; }
}
public class SharedChild
{
    public string SharedChildString { get; set; }   
}